package org.example;

import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;

import java.io.FileOutputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class WordReport {
    XWPFDocument document;
    final int PAGE_HEIGHT = 16838;
    final int PAGE_WIDTH = 11906;

    void MakeReport() {
        init();
        createCover();
        createStartContent();
        createBasicInfo();
        createGrowInfo();
        createFigureInfo();
        createEyeInfo();
        createRuTeethInfo();
        createHengTeethInfo();
        createBmiInfo();
        createJinengInfo();
        createEndContent();
        // 保存 Word 文件
        try (FileOutputStream fos = new FileOutputStream("TableExample.docx")) {
            document.write(fos);
            System.out.println("Word 文件生成成功！");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    void init() {
        document = new XWPFDocument();
        CTSectPr sectPr = document.getDocument().getBody().addNewSectPr();
        CTPageMar pageMar = sectPr.addNewPgMar();
        pageMar.setTop(1440);    // 上边距：1英寸（72点 = 1英寸，1440 = 2厘米）
        pageMar.setBottom(1440); // 下边距：1英寸
        pageMar.setLeft(1440);   // 左边距：1英寸
        pageMar.setRight(1440);  // 右边距：1英寸

        // 设置页面尺寸
        CTPageSz pageSz = sectPr.addNewPgSz();

        // 页面宽度和高度（单位是 1/20 点，A4 纵向为 11906 x 16838）
        pageSz.setW(PAGE_WIDTH); // 20cm
        pageSz.setH(PAGE_HEIGHT); // 29.7cm
    }

    //创建封面
    void createCover() {
        XWPFTable table = document.createTable();
        // 设置表格宽度为页面宽度
        table.setWidth("100%");

        XWPFTableRow row = table.getRow(0);
        row.setHeight(PAGE_HEIGHT);

        var cell = row.getCell(0);
        for (int i = 1; i < 3; i++) {
            cell.addParagraph();
        }

        var para1 = cell.addParagraph();
        para1.setAlignment(ParagraphAlignment.CENTER);

        var title = para1.createRun();
        title.setFontFamily("隶书");
        title.setFontSize(22);
        title.setText("（）学校");

        var para2 = cell.addParagraph();
        para2.setAlignment(ParagraphAlignment.CENTER);
        var subTitle = para2.createRun();
        subTitle.setFontFamily("隶书");
        subTitle.setFontSize(22);
        subTitle.setText("（）----（）年度学生健康体检");

        for (int i = 1; i < 4; i++) {
            cell.addParagraph();
        }

        var para3 = cell.addParagraph();
        para3.setAlignment(ParagraphAlignment.CENTER);
        XWPFRun content = para3.createRun();
        content.setFontFamily("隶书");
        content.setFontSize(36);
        content.setText("评");
        content.addBreak();
        content.setText("价");
        content.addBreak();
        content.setText("分");
        content.addBreak();
        content.setText("析");
        content.addBreak();
        content.setText("报");
        content.addBreak();
        content.setText("告");
        content.addBreak();


    }

    void createStartContent() {
        var para1 = document.createParagraph();
        para1.setAlignment(ParagraphAlignment.CENTER);
        var title = para1.createRun();
        title.setFontSize(22);
        title.setBold(true);
        title.setText("（）学校");

        var para2 = document.createParagraph();
        para2.setAlignment(ParagraphAlignment.CENTER);
        XWPFRun subTitle = para2.createRun();
        subTitle.setFontSize(18);
        subTitle.setText("（）----（）年度学生健康体检评价分析报告");

        document.createParagraph();
        document.createParagraph();
        document.createParagraph();

        writeContent("为了贯彻落实“学校教育教学要树立‘健康第一’的指导方针”，按照《中小学生健康体检管理办法》的要求，我所在贵校为在校学生进行了健康体检，现将有关情况报告如下：");
        document.createParagraph();
    }

    void createBasicInfo() {
        writeHeaderOne("一、基本情况");
        XWPFTable table = document.createTable();
        // 设置表格宽度为页面宽度
        table.setWidth("100%");

        // 创建表头行
        XWPFTableRow headerRow1 = table.getRow(0); // 默认创建了一行
        writeCellTextInCenter(headerRow1, 0, "年级");
        createCellWithTextInCenter(headerRow1, "班级");
        createCellWithTextInCenter(headerRow1, "男生");
        headerRow1.addNewTableCell();
        createCellWithTextInCenter(headerRow1, "女生");
        headerRow1.addNewTableCell();

        XWPFTableRow headerRow2 = table.createRow();
        writeCellTextInCenter(headerRow2, 2, "实有数");
        writeCellTextInCenter(headerRow2, 3, "检查数");
        writeCellTextInCenter(headerRow2, 4, "实有数");
        writeCellTextInCenter(headerRow2, 5, "检查数");

        mergeCellsHorizontally(table, 0, 2, 3);
        mergeCellsHorizontally(table, 0, 4, 5);

        mergeCellsVertically(table, 0, 0, 1);
        mergeCellsVertically(table, 1, 0, 1);

        //todo 填入真实数据
        for (int i = 0; i < 3; i++) {
            XWPFTableRow row = table.createRow();
            for (int j = 0; j < 6; j++) {
                writeCellTextInCenter(row, j, i + "-" + j);
            }
        }
        document.createParagraph();
        String content = "%d年----%d年度，(%s)学校检测人数为%d人，检查率为%s";
        writeContent(content);


    }

    void createGrowInfo() {
        writeHeaderOne("二、生长发育状况");

        XWPFTable table = document.createTable();
        table.setWidth("100%");

        int colCount = 13;
        // 创建表头行
        XWPFTableRow headerRow1 = table.getRow(0); // 默认创建了一行
        for (int i = 0; i < colCount - 1; i++) {
            headerRow1.createCell();
        }

        writeCellTextInCenter(headerRow1, 0, "生长发育状况分析");


        XWPFTableRow headerRow2 = table.createRow();
        writeCellTextInCenter(headerRow2, 0, "年级");
        writeCellTextInCenter(headerRow2, 1, "性别");
        writeCellTextInCenter(headerRow2, 2, "实检人数");
        writeCellTextInCenter(headerRow2, 3, "良好");
        writeCellTextInCenter(headerRow2, 5, "较好");
        writeCellTextInCenter(headerRow2, 7, "一般");
        writeCellTextInCenter(headerRow2, 9, "较差");
        writeCellTextInCenter(headerRow2, 11, "落后");

        XWPFTableRow headerRow3 = table.createRow();
        writeCellTextInCenter(headerRow3, 3, "人数");
        writeCellTextInCenter(headerRow3, 4, "%");
        writeCellTextInCenter(headerRow3, 5, "人数");
        writeCellTextInCenter(headerRow3, 6, "%");
        writeCellTextInCenter(headerRow3, 7, "人数");
        writeCellTextInCenter(headerRow3, 8, "%");
        writeCellTextInCenter(headerRow3, 9, "人数");
        writeCellTextInCenter(headerRow3, 10, "%");
        writeCellTextInCenter(headerRow3, 11, "人数");
        writeCellTextInCenter(headerRow3, 12, "%");

        mergeCellsHorizontally(table, 0, 0, 12);

        mergeCellsVertically(table, 0, 1, 2);
        mergeCellsVertically(table, 1, 1, 2);
        mergeCellsVertically(table, 2, 1, 2);

        mergeCellsHorizontally(table, 1, 3, 4);
        mergeCellsHorizontally(table, 1, 5, 6);
        mergeCellsHorizontally(table, 1, 7, 8);
        mergeCellsHorizontally(table, 1, 9, 10);
        mergeCellsHorizontally(table, 1, 11, 12);

        //todo 填入真实数据
        for (int i = 0; i < 3; i++) {
            XWPFTableRow row = table.createRow();
            for (int j = 0; j < colCount; j++) {
                writeCellTextInCenter(row, j, i + "-" + j);
            }
        }

        String strOne = "说明：通过学生健康体检分析，本校学生生长发育良好（%d）人，占受检（%s）；较好（%d）人，占受检（%s）；一般（%d）人，占受检（%s）；较差（%d）人，占受检（%s）；落后（%d）人，占受检（%s）；";
        writeContent(strOne);

    }

    void createFigureInfo() {
        document.createParagraph();
        XWPFTable table = document.createTable();
        table.setWidth("100%");

        int colCount = 9;
        // 创建表头行
        XWPFTableRow headerRow1 = table.getRow(0); // 默认创建了一行
        for (int i = 0; i < colCount - 1; i++) {
            headerRow1.createCell();
        }

        writeCellTextInCenter(headerRow1, 0, "体型状况分析");

        XWPFTableRow headerRow2 = table.createRow();
        writeCellTextInCenter(headerRow2, 0, "年级");
        writeCellTextInCenter(headerRow2, 1, "性别");
        writeCellTextInCenter(headerRow2, 2, "实检人数");
        writeCellTextInCenter(headerRow2, 3, "匀称");
        writeCellTextInCenter(headerRow2, 5, "粗壮");
        writeCellTextInCenter(headerRow2, 7, "瘦长");

        XWPFTableRow headerRow3 = table.createRow();
        writeCellTextInCenter(headerRow3, 3, "人数");
        writeCellTextInCenter(headerRow3, 4, "%");
        writeCellTextInCenter(headerRow3, 5, "人数");
        writeCellTextInCenter(headerRow3, 6, "%");
        writeCellTextInCenter(headerRow3, 7, "人数");
        writeCellTextInCenter(headerRow3, 8, "%");

        mergeCellsHorizontally(table, 0, 0, colCount - 1);

        mergeCellsVertically(table, 0, 1, 2);
        mergeCellsVertically(table, 1, 1, 2);
        mergeCellsVertically(table, 2, 1, 2);

        mergeCellsHorizontally(table, 1, 3, 4);
        mergeCellsHorizontally(table, 1, 5, 6);
        mergeCellsHorizontally(table, 1, 7, 8);

        //todo 填入真实数据
        for (int i = 0; i < 3; i++) {
            XWPFTableRow row = table.createRow();
            for (int j = 0; j < colCount; j++) {
                writeCellTextInCenter(row, j, i + "-" + j);
            }
        }

        String strTwo = "学生体型分析，匀称型（%d）人，占受检（%s）；粗壮型（%d）人，占受检（s%）；瘦长型（%d）人，占受检（s%）；";
        writeContent(strTwo);

        document.createParagraph();
        document.createParagraph();
    }

    void createEyeInfo() {
        writeHeaderOne("三、常见病检出情况");
        writeHeaderTwo("1、视力不良检出情况：");

        XWPFTable table = document.createTable();
        table.setWidth("100%");

        int colCount = 13;
        // 创建表头行
        XWPFTableRow headerRow1 = table.getRow(0); // 默认创建了一行
        for (int i = 0; i < colCount - 1; i++) {
            headerRow1.createCell();
        }
        writeCellTextInCenter(headerRow1, 0, "视力不良检出情况");

        XWPFTableRow headerRow2 = table.createRow();
        writeCellTextInCenter(headerRow2, 0, "年级");
        writeCellTextInCenter(headerRow2, 1, "性别");
        writeCellTextMultiLine(headerRow2, 2, Arrays.asList("实检", "人数"));
        writeCellTextInCenter(headerRow2, 3, "视力低下");


        XWPFTableRow headerRow3 = table.createRow();
        headerRow3.setHeight(20 * 50);
        writeCellTextMultiLine(headerRow3, 3, Arrays.asList("轻度低下", "(4.9)"));
        writeCellTextMultiLine(headerRow3, 5, Arrays.asList("中度低下", "(4.6--4.8)"));
        writeCellTextMultiLine(headerRow3, 7, Arrays.asList("重度低下", "(4.5及以下)"));
        writeCellTextInCenter(headerRow3, 9, "合计");
        writeCellTextInCenter(headerRow3, 11, "视力正常");

        XWPFTableRow headerRow4 = table.createRow();
        writeCellTextInCenter(headerRow4, 3, "人数");
        writeCellTextInCenter(headerRow4, 4, "%");
        writeCellTextInCenter(headerRow4, 5, "人数");
        writeCellTextInCenter(headerRow4, 6, "%");
        writeCellTextInCenter(headerRow4, 7, "人数");
        writeCellTextInCenter(headerRow4, 8, "%");
        writeCellTextInCenter(headerRow4, 9, "人数");
        writeCellTextInCenter(headerRow4, 10, "%");
        writeCellTextInCenter(headerRow4, 11, "人数");
        writeCellTextInCenter(headerRow4, 12, "%");

        mergeCellsVertically(table, 0, 1, 3);
        mergeCellsVertically(table, 1, 1, 3);
        mergeCellsVertically(table, 2, 1, 3);

        mergeCellsHorizontally(table, 0, 0, colCount - 1);

        mergeCellsHorizontally(table, 1, 3, 10);
        mergeCellsHorizontally(table, 1, 11, 12);

        mergeCellsHorizontally(table, 2, 3, 4);
        mergeCellsHorizontally(table, 2, 5, 6);
        mergeCellsHorizontally(table, 2, 7, 8);
        mergeCellsHorizontally(table, 2, 9, 10);
        mergeCellsHorizontally(table, 2, 11, 12);

        //todo 填入真实数据
        for (int i = 0; i < 3; i++) {
            XWPFTableRow row = table.createRow();
            for (int j = 0; j < colCount; j++) {
                writeCellTextInCenter(row, j, i + "-" + j);
            }
        }


    }

    void createRuTeethInfo() {
        writeHeaderTwo("2、龋齿检出情况：");

        XWPFTable table = document.createTable();
        table.setWidth("100%");

        int colCount = 15;
        int lastColIndex = colCount - 1;

        // 创建表头行
        XWPFTableRow headerRow1 = table.getRow(0); // 默认创建了一行
        for (int i = 0; i < colCount - 1; i++) {
            headerRow1.createCell();
        }
        writeCellTextInCenter(headerRow1, 0, "乳牙龋齿检出情况");

        XWPFTableRow headerRow2 = table.createRow();
        writeCellTextInCenter(headerRow2, 0, "年级");
        writeCellTextInCenter(headerRow2, 1, "性别");
        writeCellTextMultiLine(headerRow2, 2, Arrays.asList("实检", "人数"));
        writeCellTextInCenter(headerRow2, 3, "龋齿患失补情况");

        XWPFTableRow headerRow3 = table.createRow();
        writeCellTextInCenter(headerRow3, 3, "龋患率");
        writeCellTextInCenter(headerRow3, 5, "龋均");
        writeCellTextInCenter(headerRow3, 7, "充填率");
        writeCellTextInCenter(headerRow3, 9, "乳龋患失补");

        XWPFTableRow headerRow4 = table.createRow();
        writeCellTextMultiLine(headerRow4, 3, Arrays.asList("龋牙", "人数"));
        writeCellTextMultiLine(headerRow4, 4, Arrays.asList("占受", "检%"));
        writeCellTextInCenter(headerRow4, 5, "龋牙数");
        writeCellTextInCenter(headerRow4, 6, "均值");

        writeCellTextMultiLine(headerRow4, 7, Arrays.asList("龋补", "牙数"));
        writeCellTextMultiLine(headerRow4, 8, Arrays.asList("占龋", "牙%"));
        writeCellTextMultiLine(headerRow4, 9, Arrays.asList("乳龋", "患牙", "数"));
        writeCellTextMultiLine(headerRow4, 10, Arrays.asList("乳龋", "患人", "数"));

        writeCellTextMultiLine(headerRow4, 11, Arrays.asList("乳龋", "失牙", "数"));
        writeCellTextMultiLine(headerRow4, 12, Arrays.asList("乳龋", "失人", "数"));
        writeCellTextMultiLine(headerRow4, 13, Arrays.asList("乳龋", "补牙", "数"));
        writeCellTextMultiLine(headerRow4, 14, Arrays.asList("乳龋", "补人", "数"));

        mergeCellsVertically(table, 0, 1, 3);
        mergeCellsVertically(table, 1, 1, 3);
        mergeCellsVertically(table, 2, 1, 3);

        mergeCellsHorizontally(table, 0, 0, lastColIndex);

        mergeCellsHorizontally(table, 1, 3, lastColIndex);

        mergeCellsHorizontally(table, 2, 3, 4);
        mergeCellsHorizontally(table, 2, 5, 6);
        mergeCellsHorizontally(table, 2, 7, 8);
        mergeCellsHorizontally(table, 2, 9, lastColIndex);

    }

    void createHengTeethInfo() {
        XWPFTable table = document.createTable();
        table.setWidth("100%");

        int colCount = 15;
        int lastColIndex = colCount - 1;

        // 创建表头行
        XWPFTableRow headerRow1 = table.getRow(0); // 默认创建了一行
        for (int i = 0; i < colCount - 1; i++) {
            headerRow1.createCell();
        }
        writeCellTextInCenter(headerRow1, 0, "恒牙龋齿检出情况");

        XWPFTableRow headerRow2 = table.createRow();
        writeCellTextInCenter(headerRow2, 0, "年级");
        writeCellTextInCenter(headerRow2, 1, "性别");
        writeCellTextMultiLine(headerRow2, 2, Arrays.asList("实检", "人数"));
        writeCellTextInCenter(headerRow2, 3, "龋齿患失补情况");

        XWPFTableRow headerRow3 = table.createRow();
        writeCellTextInCenter(headerRow3, 3, "龋患率");
        writeCellTextInCenter(headerRow3, 5, "龋均");
        writeCellTextInCenter(headerRow3, 7, "充填率");
        writeCellTextInCenter(headerRow3, 9, "恒龋患失补");

        XWPFTableRow headerRow4 = table.createRow();
        writeCellTextMultiLine(headerRow4, 3, Arrays.asList("龋牙", "人数"));
        writeCellTextMultiLine(headerRow4, 4, Arrays.asList("占受", "检%"));
        writeCellTextInCenter(headerRow4, 5, "龋牙数");
        writeCellTextInCenter(headerRow4, 6, "均值");

        writeCellTextMultiLine(headerRow4, 7, Arrays.asList("龋补", "牙数"));
        writeCellTextMultiLine(headerRow4, 8, Arrays.asList("占龋", "牙%"));
        writeCellTextMultiLine(headerRow4, 9, Arrays.asList("恒龋", "患牙", "数"));
        writeCellTextMultiLine(headerRow4, 10, Arrays.asList("恒龋", "患人", "数"));

        writeCellTextMultiLine(headerRow4, 11, Arrays.asList("恒龋", "失牙", "数"));
        writeCellTextMultiLine(headerRow4, 12, Arrays.asList("恒龋", "失人", "数"));
        writeCellTextMultiLine(headerRow4, 13, Arrays.asList("恒龋", "补牙", "数"));
        writeCellTextMultiLine(headerRow4, 14, Arrays.asList("恒龋", "补人", "数"));

        mergeCellsVertically(table, 0, 1, 3);
        mergeCellsVertically(table, 1, 1, 3);
        mergeCellsVertically(table, 2, 1, 3);

        mergeCellsHorizontally(table, 0, 0, lastColIndex);

        mergeCellsHorizontally(table, 1, 3, lastColIndex);

        mergeCellsHorizontally(table, 2, 3, 4);
        mergeCellsHorizontally(table, 2, 5, 6);
        mergeCellsHorizontally(table, 2, 7, 8);
        mergeCellsHorizontally(table, 2, 9, lastColIndex);
    }

    void createBmiInfo(){
        writeHeaderTwo("3、营养状况分析：");
        var table = document.createTable();
        table.setWidth("100%");



        XWPFTableRow headerRow1 = table.getRow(0); // 默认创建了一行
        writeCellTextInCenter(headerRow1, 0, "年级");
        createCellWithTextInCenter(headerRow1, "性别");
        createCellWithTextInCenter(headerRow1, "实检人数");
        createCellWithTextInCenter(headerRow1, "正常");
        headerRow1.addNewTableCell();
        createCellWithTextInCenter(headerRow1, "肥胖");
        headerRow1.addNewTableCell();
        createCellWithTextInCenter(headerRow1, "超重");
        headerRow1.addNewTableCell();
        createCellWithTextInCenter(headerRow1, "生长迟缓");
        headerRow1.addNewTableCell();
        createCellWithTextInCenter(headerRow1, "轻度消瘦");
        headerRow1.addNewTableCell();
        createCellWithTextInCenter(headerRow1, "中重度消瘦");
        headerRow1.addNewTableCell();



        XWPFTableRow headerRow2 = table.createRow();
        writeCellTextInCenter(headerRow2, 3, "人数");
        writeCellTextInCenter(headerRow2, 4, "%");
        writeCellTextInCenter(headerRow2, 5, "人数");
        writeCellTextInCenter(headerRow2, 6, "%");
        writeCellTextInCenter(headerRow2, 7, "人数");
        writeCellTextInCenter(headerRow2, 8, "%");

        writeCellTextInCenter(headerRow2, 9, "人数");
        writeCellTextInCenter(headerRow2, 10, "%");
        writeCellTextInCenter(headerRow2, 11, "人数");
        writeCellTextInCenter(headerRow2, 12, "%");
        writeCellTextInCenter(headerRow2, 13, "人数");
        writeCellTextInCenter(headerRow2, 14, "%");

        mergeCellsVertically(table,0,0,1);
        mergeCellsVertically(table,1,0,1);
        mergeCellsVertically(table,2,0,1);

        mergeCellsHorizontally(table,0,3,4);
        mergeCellsHorizontally(table,0,5,6);
        mergeCellsHorizontally(table,0,7,8);
        mergeCellsHorizontally(table,0,9,10);
        mergeCellsHorizontally(table,0,11,12);
        mergeCellsHorizontally(table,0,13,14);

        String content="全校男女生合计，肥胖检出率为24.03%，超重检出率为15.75%，正常体重检出率为55.80%，生长迟缓检出率为0.22%，轻度消瘦检出率为2.56%, 中重度消瘦检出率为1.63%；";
        writeContent(content);

    }

    void createJinengInfo(){
        writeHeaderTwo("5、形态机能指标比较");
        writeContent("通过体检数据显示各年龄段学生形态、机能指标的发育符合学生生长发育的一般规律。身高、体重、肺活量随年龄增长而增长，同时，体重和胸围却出现明显增长，这显示学生的体形出现向超重及肥胖发展的趋势，这与目前生活水平提高、生活节奏加快、生活方式改变，摄入过多高能量食物，对身体锻炼重视程度不够，缺乏有效锻炼均有直接关系，符合肥胖和超重在学生中发生率逐年提高的趋势，这应该引起我们的高度重视。应该通过对学生营养餐不断改进，加强对学生营养知识的教育，改变其不良饮食习惯及加强校内外的体育锻炼等措施，以降低超重和肥胖的发生率，从而减低其到成年后心血管、糖尿病等慢性病的发生率。");

        var table = document.createTable();
        XWPFTableRow headerRow1 = table.getRow(0); // 默认创建了一行
        writeCellTextInCenter(headerRow1, 0, "年级");
        createCellWithTextInCenter(headerRow1, "性别");
        createCellWithTextInCenter(headerRow1, "实检人数");
        createCellWithTextInCenter(headerRow1, "身高(cm)");
        headerRow1.addNewTableCell();
        createCellWithTextInCenter(headerRow1, "体重(kg)");
        headerRow1.addNewTableCell();
        createCellWithTextInCenter(headerRow1, "肺活量(ml)");
        headerRow1.addNewTableCell();
        createCellWithTextInCenter(headerRow1, "收缩压(mmHg)");
        headerRow1.addNewTableCell();
        createCellWithTextInCenter(headerRow1, "舒张压(mmHg)");
        headerRow1.addNewTableCell();
        createCellWithTextInCenter(headerRow1, "肺活量体重指数");
        headerRow1.addNewTableCell();

        XWPFTableRow headerRow2 = table.createRow();
        writeCellTextInCenter(headerRow2, 3, "平均数");
        writeCellTextInCenter(headerRow2, 4, "标准差");
        writeCellTextInCenter(headerRow2, 5, "平均数");
        writeCellTextInCenter(headerRow2, 6, "标准差");
        writeCellTextInCenter(headerRow2, 7, "平均数");
        writeCellTextInCenter(headerRow2, 8, "标准差");

        writeCellTextInCenter(headerRow2, 9, "平均数");
        writeCellTextInCenter(headerRow2, 10, "标准差");
        writeCellTextInCenter(headerRow2, 11, "平均数");
        writeCellTextInCenter(headerRow2, 12, "标准差");
        writeCellTextInCenter(headerRow2, 13, "平均数");
        writeCellTextInCenter(headerRow2, 14, "标准差");

        mergeCellsVertically(table,0,0,1);
        mergeCellsVertically(table,1,0,1);
        mergeCellsVertically(table,2,0,1);

        mergeCellsHorizontally(table,0,3,4);
        mergeCellsHorizontally(table,0,5,6);
        mergeCellsHorizontally(table,0,7,8);
        mergeCellsHorizontally(table,0,9,10);
        mergeCellsHorizontally(table,0,11,12);
        mergeCellsHorizontally(table,0,13,14);

    }

    void createEndContent(){
        writeHeaderOne("四、总结与分析与建议");
        writeHeaderTwo("1、总结与分析");
        String content1= """
                本学年，全校学生肥胖检出率为（  24.03%    ），学生肥胖可引起呼吸系统和心血管功能降低，使学生产生压抑、焦虑、缺乏自信等不良心理情绪，是形成很多成年期疾病的危害因素。
                对肥胖的学生最主要的是应指导她们合理膳食，减少热量的摄入，适当增加运动量。
                """;
        writeContent(content1);

        String content2= """
                本学年学生超重检出率为（   15.75%   ），对于超重的同学可以分为两种情况，一是由于经常锻炼，或者身体骨架相对较大，骨骼和肌肉发达而形成超重，
                所以，这部分青少年不应该作为减肥对象。另一种是膳食摄入过多、体力活动过少，正出现肥胖倾向的青少年，对这部分学生应该加强营养教育，及早预防肥胖。
                """;
        writeContent(content2);

        String content3= """
                同时，我们还应该关注学生较低体重的问题。较低体重按照国际通行标准仍然属于轻度营养不良范畴，表面上未造成机体损伤，实际上对长期记忆力、
                快速思维、逻辑思维仍然有很大影响。所以，学校应根据本校学生营养不良状况采取干预措施，培养良好的饮食习惯，均衡膳食。
                """;
        writeContent(content3);

        String content4= """
                对于查出体检异常的同学应该按照医生建议到医院检查或咨询。学校应加强与家长的联系，保证学生及时就医，并及时追踪学生病情动态，以确保学生的身心健康。
                """;
        writeContent(content4);


        writeHeaderTwo("2、工作指导建议");

        writeContent("1.加强学生用眼卫生知识的宣传，做好视力保护工作；");
        writeContent("2.强化保护视力、预防近视的知识，培养学生良好的用眼卫生习惯；");
        writeContent("3.控制学生近视工作的时间和强度，避免视力负荷过重，减轻学生课业负担，保证学生有足够的休息、睡眠和课外活动时间。");
        writeContent("""
4.改善学生的学习环境，学生视力下降的发生、发展与教室采光照明条件和就坐的课桌椅有密切的关系，所以学校在改善教室采光照明条件的同时，
应重视学生身高与课桌椅搭配的问题，只有使学生坐在符合他们身高的课桌椅上，才能有效的预防和控制视力不良和脊柱异常弯曲的发生发展。
""");
        writeContent("5.坚持每天做眼保健操，做操时穴位要准确、动作要到位。");
        writeContent("6.定期检查学生的视力，了解视力变化情况，及时发现问题，制定相应措施。");
        writeContent("7.合理膳食，加强营养卫生指导。");
        writeContent("8.肥胖学生主要是缺乏合理营养知识，热量摄取过多，尤其是甜食摄入过多，同时课业负担过重，体育锻炼时间过少，降低了热量的消耗，也是原因之一，对患肥胖症的学生要进行膳食指导，鼓励其多参加体育锻炼。");
        writeContent("9.定期查治患病学生，观察疗效，对患病学生的检查频率不得少于每年一次。");
        writeContent("10.对患有轻度营养不良的学生，学校应与家长合作，进行膳食指导，对患有重度营养不良的学生，学校应通知家长，去医院就诊。");
        writeContent("11.加强龋病的综合防治。");
        writeContent("""
                11.1龋病是影响儿童青少年健康和生长发育最常见的口腔疾病。龋病的防治工作，应首先从健康教育入手，提高学生口腔健康意识，
                使学生知道龋病的危害及预防等口腔保健知识，培养学生良好的口腔卫生习惯，掌握正确的刷牙方法，养成良好的刷牙习惯，做到饭后漱口，早晚刷牙。
                """);
        writeContent("11.2龋病是一种非自愈性疾病，应及早治疗。对活动龋要尽早充填。本次体检查出龋患学生：");
        writeContent("11.3对龋病好发部位采取窝沟封闭方法，预防牙齿咬合面龋齿。");
        writeContent("12.继续做好沙眼防治工作。");
        writeContent("12.1对沙眼和疑沙的学生应及时通知家长，积极进行治疗。");
        writeContent("13.其他检出的疾病和阳性体征的防治意见");
        writeContent("13.1心血管疾病的学生应定期诊治，避免参加剧烈运动，预防感冒。血压偏高的学生应注意观察，如果继续存在，应到医院作进一步诊治。边缘性杂音的学生，应进一步到医院检查，以便得到及时确诊和治疗。");
        writeContent("""
                13.2脊柱弯曲异常的学生应引起重视，注意读写姿势。教育学生注意端正身体姿势，行走、读写时胸部自然挺直，两肩齐平向后扩展，
                站立时重心在两脚上。对脊柱异常者，根据脊柱弯曲的方向及部位进行矫正操治疗，如脊柱向左侧弯者，多做上半身向左弯的运动，
                右侧弯者多做右弯运动。胸段弯曲者，只弯曲胸段，腰段弯曲者，只弯曲腰段。对于脊柱度Ⅱ以上弯曲者，应到医院进一步诊治。
                """);
        writeContent("""
                14.指导血压偏高者，养成低盐饮食习惯，每天食盐量不超过5克，避免过度的情绪激动或精神紧张，每天保证有足够的睡眠；血压偏低者，坚持早餐，不挑食、偏食，适当补充维生素C、B族和维生素PP等，起床走动及体位改变时，动作要慢，避免骤然起立。""");
    }

    private void mergeCellsHorizontally(XWPFTable table, int row, int fromColumn, int toColumn) {
        XWPFTableRow tableRow = table.getRow(row);
        for (int colIndex = fromColumn; colIndex <= toColumn; colIndex++) {
            XWPFTableCell cell = tableRow.getCell(colIndex);
            if (colIndex == fromColumn) {
                // 起始列设置为 RESTART
                cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART);
            } else {
                // 其他列设置为 CONTINUE
                cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.CONTINUE);
            }
        }
    }

    private void mergeCellsVertically(XWPFTable table, int col, int fromRow, int toRow) {
        for (int rowIndex = fromRow; rowIndex <= toRow; rowIndex++) {
            XWPFTableCell cell = table.getRow(rowIndex).getCell(col);
            if (rowIndex == fromRow) {
                cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.RESTART);
            } else {
                cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.CONTINUE);
            }
        }
    }

    private void writeContent(String text) {
        XWPFParagraph paragraph = document.createParagraph();
        paragraph.setAlignment(ParagraphAlignment.BOTH);
        paragraph.setIndentationFirstLine(570);

        CTPPr pPr = paragraph.getCTP().getPPr();

        CTSpacing spacing = pPr.isSetSpacing() ? pPr.getSpacing() : pPr.addNewSpacing();
        spacing.setLineRule(STLineSpacingRule.AUTO); // 自动行距（倍数）
        spacing.setLine(BigInteger.valueOf(288)); // 1.2倍行距 (240表示1.2倍)


        var run = paragraph.createRun();
        run.setFontFamily("华文中宋");
        run.setFontSize(14);
        run.setText(text);
    }

    private void writeHeaderOne(String text) {
        var para = document.createParagraph();
        para.setStyle("Heading1");
        var run = para.createRun();
        run.setBold(true);
        run.setFontSize(22);
        run.setText(text);
        document.createParagraph();
        document.createParagraph();
    }

    private void writeHeaderTwo(String text) {
        var para = document.createParagraph();
        para.setStyle("Heading2");
        var run = para.createRun();
        run.setBold(true);
        run.setFontSize(16);
        run.setText(text);
        document.createParagraph();
        document.createParagraph();
    }

    private void createCellWithTextInCenter(XWPFTableRow row, String text) {
        var cell = newVerCenterCell(row);
        setTextHC(cell, text);
    }

    private void writeCellTextInCenter(XWPFTableRow row, int pos, String text) {
        var cell = makeCellVerCenter(row.getCell(pos));
        setTextHC(cell, text);
    }

    private void writeCellTextMultiLine(XWPFTableRow row, int pos, List<String> lines) {
        var cell = row.getCell(pos);
        makeCellVerCenter(cell);

        XWPFParagraph para = cell.addParagraph();
        para.setAlignment(ParagraphAlignment.CENTER);
        var run = para.createRun();
        for (String s : lines) {
            run.setText(s);
            run.addBreak();
        }
    }

    private XWPFTableCell newVerCenterCell(XWPFTableRow row) {
        var cell = row.createCell();
        makeCellVerCenter(cell);
        return cell;
    }

    private XWPFTableCell makeCellVerCenter(XWPFTableCell cell) {
        CTTcPr tcPr = cell.getCTTc().addNewTcPr();
        tcPr.addNewVAlign().setVal(org.openxmlformats.schemas.wordprocessingml.x2006.main.STVerticalJc.CENTER);
        return cell;
    }

    private void setTextHC(XWPFTableCell cell, String text) {
        var paras = cell.getParagraphs();
        var para = paras.get(0);
        para.setAlignment(ParagraphAlignment.CENTER);
        cell.setText(text);
    }

}
